package cn.myspringboot.system.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.system.service.RoleService;
import cn.myspringboot.user.domain.Role;
import cn.myspringboot.user.query.RoleQuery;
import cn.myspringboot.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("角色相关的接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @PreAuthorize(name = "添加或修改角色信息",sn = "role:insert",descs ="添加或修改角色信息")
    @ApiOperation("添加或修改角色信息")
    @PostMapping
    public R insert(@RequestBody Role role) {
        if(role.getId() == null){
            roleService.add(role);
        }else {
            roleService.update(role);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除角色信息",sn = "role:delete",descs ="根据id删除角色信息")
    @ApiOperation("根据id删除角色信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        roleService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除角色信息",sn = "role:delBatch",descs ="批量删除角色信息")
    @ApiOperation("批量删除角色信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        roleService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询角色信息",sn = "role:getById",descs ="根据id查询角色信息")
    @ApiOperation("根据id查询角色信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(roleService.getById(id));
    }
    @PreAuthorize(name = "查询所有角色信息",sn = "role:getAll",descs ="查询所有角色信息")
    @ApiOperation("查询所有角色信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(roleService.getAll());
    }
    @PreAuthorize(name = "分页查询角色信息",sn = "role:page",descs ="分页查询角色信息")
    @ApiOperation("分页查询角色信息")
    @PostMapping("/page")
    public R page(@RequestBody RoleQuery roleQuery) {
        PageHelper.startPage(roleQuery.getPage(), roleQuery.getSize());
        List<Role> roles = roleService.selectRolePage(roleQuery);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return R.ok(pageInfo);
    }
}
